﻿namespace Microsoft.ExternalResearch.CreativeCommons.PowerPoint
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Interop.PowerPoint;
    using System.Runtime.InteropServices;
    using Microsoft.Office.Core;
    using Microsoft.ExternalResearch.CreativeCommons.Common;
    using System.IO;
    using System.Xml;

    /// <summary>
    /// Represents the Creative Commons Add-in for Microsoft PowerPoint.
    /// </summary>
    /// 
    public partial class ThisAddIn
    {
        /// <summary>
        /// Represents the PowerPoint application object.
        /// </summary>
        private static Application powerPointApplication;


        /// <summary>
        /// Raised when the application starts.
        /// </summary>
        public static event EventHandler ApplicationStartedEvent;

        /// <summary>
        /// Gets or sets the PowerPoint application object.
        /// </summary>
        public static Application PowerPointApplication
        {
            get
            {
                return powerPointApplication;
            }

            set
            {
                powerPointApplication = value;
            }
        }

        /// <summary>
        /// Raises the ApplicationStarted event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void ThisAddInStartup(object sender, System.EventArgs e)
        {
            ThisAddIn.PowerPointApplication = this.Application;

            if (null != ApplicationStartedEvent)
            {
                ApplicationStartedEvent(this, null);
            }
        }
        /// <summary>
        /// Gets a value indicating whether a presenttaion is opened.
        /// </summary>
        public static bool HasPresentation()
        {
            bool hasPresentation = false;

            if (ThisAddIn.PowerPointApplication.Presentations.Count > 0 && null != ThisAddIn.PowerPointApplication.ActivePresentation)
            {
                hasPresentation = true;
            }

            return hasPresentation;
        }

        /// <summary>
        /// Removes the presentation property for Creative Commons license.
        /// </summary>
        public static void RemoveLicenseFromDocument()
        {
            DocumentProperties customProperties = (DocumentProperties)ThisAddIn.PowerPointApplication.ActivePresentation.CustomDocumentProperties;

            if (null != Helper.ReadLicenseInfo(customProperties))
            {
                customProperties[Helper.LicenseIdPropertyName].Delete();
                customProperties[Helper.LicenseUrlPropertyName].Delete();
                customProperties[Helper.LicenseXmlPropertyName].Delete();
            }
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddInStartup);
        }

        #endregion
    }
}
